package org.wishwork.helper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.wishwork.JSTService;
import org.wishwork.api.Base;
import org.wishwork.api.ExtendBase;
import org.wishwork.api.token.get.ResponseToken;
import org.wishwork.cache.DefaultMemoryCacheOperator;
import org.wishwork.constant.Constants;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: WangYX
 * @create: 2022-04-02 15:15
 * @Version: 1.0.0
 **/

public class HttpHelper {

    /**
     * 发送请求
     *
     * @param accessToken token
     * @param biz         请求参数的map的json字符串
     * @param requestUrl  第三方请求的url
     * @return
     * @throws IOException
     */
    public static String sendRequest(String accessToken, String biz, String requestUrl) throws IOException {
        // 公共请求参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("app_key", JSTService.context.properties().getAppKey());
        params.put("access_token", accessToken);
        params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        params.put("version", "2");
        params.put("charset", "utf-8");
        // 业务参数
        params.put("biz", biz);
        // 签名参数
        params.put("sign", JuShuiTanHelper.signTopRequest(params, JSTService.context.properties().getAppSecret(), JuShuiTanHelper.SIGN_METHOD_MD5));
        String url = JSTService.context.properties().getServerUrl() + requestUrl;

        String query = JuShuiTanHelper.buildQuery(params);
        return HttpUtil.post(url, query);
    }

    /**
     * 发送请求
     *
     * @param biz        请求参数的map的json字符串
     * @param requestUrl 第三方请求的url
     * @return
     * @throws IOException
     */
    public static String sendRequest(String biz, String requestUrl) throws IOException {
        ResponseToken token = (ResponseToken) DefaultMemoryCacheOperator.instance().get(Constants.TOKEN);
        if (BeanUtil.isEmpty(token)) {
            throw new JSTException(ExceptionEnum.USER_ERROR_A0223, "TOKEN IS NULL!!!");
        }
        return sendRequest(token.getAccessToken(), biz, requestUrl);
    }

    /**
     * 发送请求
     *
     * @param params     请求参数的map
     * @param requestUrl 第三方请求的url
     * @return
     * @throws IOException
     */
    public static String sendRequest(Map<String, Object> params, String requestUrl) throws IOException {
        String biz = JSON.toJSONString(params);
        return sendRequest(biz, requestUrl);
    }

    /**
     * 发送请求
     *
     * @param t          继承Base类的对象
     * @param params     请求参数的map
     * @param requestUrl 第三方请求的url
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T extends Base> String sendRequest(T t, Map<String, Object> params, String requestUrl) throws IOException {
        if (t.getPageIndex() != null) {
            params.put("page_index", t.getPageIndex());
        }
        if (t.getPageSize() != null) {
            params.put("page_size", t.getPageSize());
        }
        return sendRequest(params, requestUrl);
    }

    /**
     * 发送请求
     *
     * @param t          继承ExtendBase类的对象
     * @param params     请求参数的map
     * @param requestUrl 第三方请求的url
     * @param <T>
     * @return
     * @throws IOException
     */
    public static <T extends ExtendBase> String sendRequest(T t, Map<String, Object> params, String requestUrl) throws IOException {
        if (t.getPageIndex() != null) {
            params.put("page_index", t.getPageIndex());
        }
        if (t.getPageSize() != null) {
            params.put("page_size", t.getPageSize());
        }
        if (!StrUtil.isEmpty(t.getModifiedBegin())) {
            params.put("modified_begin", t.getModifiedBegin());
        }
        if (!StrUtil.isEmpty(t.getModifiedEnd())) {
            params.put("modified_end", t.getModifiedEnd());
        }
        return sendRequest(params, requestUrl);
    }


}
